<?php
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busResource.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/business/busUser.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/daoItem.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/daoUpdate.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/daoFavourite.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/dao/daoRating.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/item.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/favourite.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/rating.php';
require_once $ubi['SERVER']['SERVER_ROOT'] . '/model/update.php';

require_once $ubi['SERVER']['SERVER_ROOT'] . '/utility/sendMail.php';

class ItemBus {	
	private $_itemDao;
	private $_updateDao;	
	private $_ratingDao;
	private $_favouriteDao;
	private $_userBus;
		
	public function __construct() {
		$this->_itemDao = new ItemDao();
		$this->_updateDao = new UpdateDao();		
		$this->_ratingDao = new RatingDao();
		$this->_favouriteDao = new FavouriteDao();		
		$this->_userBus = new UserBus();
	}
	
	public function addItem($item) {
		global $ubi_score;
		
		$item->setAnswerList(GeneralFunctions::stripString($item->getAnswerList()));
		$item->setBeginPrice(GeneralFunctions::stripString($item->getBeginPrice()));
		$item->setCategoryList(GeneralFunctions::stripString($item->getCategoryList()));
		$item->setEndPrice(GeneralFunctions::stripString($item->getEndPrice()));
		$item->setUnit(GeneralFunctions::stripString($item->getUnit()));
		
		$currentTime = $_SERVER['REQUEST_TIME'];		
		
		$item->setAccessedDate($currentTime);
		$item->setCreatedDate($currentTime);
		$item->setModifiedDate($currentTime);
		$item->setDateOfPrice($currentTime);
		
		$id = $this->_itemDao->addItem($item);
		if ($id) {
			$this->_userBus->addScore($item->getUserID(), $ubi_score['ONE_POST']);
			
			$update = new UpdateModel();
			$update->setIndicteeID($itemID);
			$update->setModifiedDate($currentTime);
			$update->setUpdateType($ubi['UPDATE']['POST_ITEM']);
			$update->setUserID($userID);
			$update->setLink('index.php?view&type=item&id=' . $itemID);
			$update->setName($item->getItem());
			
			$this->_updateDao->addUpdate($update);
		}
		return $id;
	}
	
	public function getItemsByUserID($userid) {
		return $this->_itemDao->getItemsByUserId($userid);
	}
	
	public function getItemsByUserIDOfPage($userID, $page) {
		global $ubi;
		$from = ($page - 1) * $ubi['SITE']['USER_NUMBER_POST'];
		$offset = $ubi['SITE']['USER_NUMBER_POST'];
		
		return $this->_itemDao->getItemsByUserIDFromTo($userID, $from, $offset);
	}
	
	public function getNumberOfItemsByUserID($userID) {
		return $this->_itemDao->getNumberOfItemsByUserId($userID);
	}
	
	public function getItem($itemID) {
		return $this->_itemDao->getItem($itemID);
	}
	
	public function addResource($item, $resourceID) {
		$id = $this->_itemDao->addResource($itemID, $resourceID);
		if ($id) {
			global $ubi_score;
			$this->_userBus->addScore($item->getUserID(), $ubi_score['UPLOAD_PIC']);
		}
		return $id;		
	}
	
	public function isFavourite($itemID, $userID) {
		global $ubi;

		return $this->_favouriteDao->isFavourite($userID, $ubi['ITEM']['VALUE'], $itemID);
	}
	
	public function isPlus($itemID, $userID) {
		global $ubi;

		$rating = new RatingModel();
		$rating->setCreatedDate($_SERVER['REQUEST_TIME']);
		$rating->setIndicteeID($itemID);
		$rating->setInteractType($ubi['ITEM']['VALUE']);
		$rating->setRatingValue($ubi['RATING']['PLUS']);
		$rating->setUserID($userID);
		
		return $this->_ratingDao->isRating($rating);
	}
	
	public function isMinus($itemID, $userID) {
		global $ubi;

		$rating = new RatingModel();
		$rating->setCreatedDate($_SERVER['REQUEST_TIME']);
		$rating->setIndicteeID($itemID);
		$rating->setInteractType($ubi['ITEM']['VALUE']);
		$rating->setRatingValue($ubi['RATING']['MINUS']);
		$rating->setUserID($userID);
		
		return $this->_ratingDao->isRating($rating);
	}
	
	public function plus($itemID, $userID) {
		global $ubi;

		$rating = new RatingModel();
		$rating->setCreatedDate($_SERVER['REQUEST_TIME']);
		$rating->setIndicteeID($itemID);
		$rating->setInteractType($ubi['ITEM']['VALUE']);
		$rating->setRatingValue($ubi['RATING']['PLUS']);
		$rating->setUserID($userID);
		
		$ck = $this->_ratingDao->doRateItem($rating);

		if ($ck) {
			// add update
			$update = new UpdateModel();
			$update->setIndicteeID($itemID);
			$update->setModifiedDate($rating->getCreatedDate());
			$update->setUpdateType($ubi['UPDATE']['PLUS_ITEM']);
			$update->setUserID($userID);
			$update->setLink('index.php?view&type=item&id=' . $itemID);
			$update->setName($this->_itemDao->getItem($itemID)->getItem());
			
			$this->_updateDao->addUpdate($update);
			
			$post = $this->_itemDao->getItem($itemID);
			$ownerPost = $this->_userBus->getUser($post->getUserID());
			if (!in_array($ubi['EMAIL']['PLUS_MY_POST'], $ownerPost->getEmailPrivacies())) {
				$sender = $this->_userBus->getUser($userID);
				SendMail::getEmailTemplate($ubi['EMAIL']['PLUS_MY_POST'], $template, $subject);
				
				$arrParam = array();
				$arrParam[] = $ownerPost->getFirstName() . ' ' . $ownerPost->getLastName();	
				$arrParam[] = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . 'index.php?view&type=user&userid=' . $sender->getUserID() . '">' . $sender->getUserName() . '</a>';
				$arrParam[] = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . 'index.php?view&type=item&id=' . $post->getItemID() . '">' . $post->getItem() . '</a>';
				$arrParam[] = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . '">' . $ubi['SERVER']['SITE_NAME'] . '</a>';
				SendMail::sendEMail($template, $subject, $arrParam, $ownerPost->getEmail());
			}
			
			// add score
			global $ubi_score;
			$this->_userBus->addScore($userID, $ubi_score['PLUS_MINUS']);
		}
		
		return $ck;				
	}
	
	public function minus($itemID, $userID) {
		global $ubi;

		$rating = new RatingModel();
		$rating->setCreatedDate($_SERVER['REQUEST_TIME']);
		$rating->setIndicteeID($itemID);
		$rating->setInteractType($ubi['ITEM']['VALUE']);
		$rating->setRatingValue($ubi['RATING']['MINUS']);
		$rating->setUserID($userID);
		
		$ck = $this->_ratingDao->doRateItem($rating);

		if ($ck) {
			// add update			
			$update = new UpdateModel();
			$update->setIndicteeID($itemID);
			$update->setModifiedDate($rating->getCreatedDate());
			$update->setUpdateType($ubi['UPDATE']['MINUS_ITEM']);
			$update->setUserID($userID);
			$update->setLink('index.php?view&type=item&id=' . $itemID);
			$update->setName($this->_itemDao->getItem($itemID)->getItem());
			
			$this->_updateDao->addUpdate($update);			
			
			// send mail for the owner
			$post = $this->_itemDao->getItem($itemID);
			$ownerPost = $this->_userBus->getUser($post->getUserID());
			if (!in_array($ubi['EMAIL']['MINUS_MY_POST'], $ownerPost->getEmailPrivacies())) {
				$sender = $this->_userBus->getUser($userID);
				SendMail::getEmailTemplate($ubi['EMAIL']['MINUS_MY_POST'], $template, $subject);
				
				$arrParam = array();
				$arrParam[] = $ownerPost->getFirstName() . ' ' . $ownerPost->getLastName();	
				$arrParam[] = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . 'index.php?view&type=user&userid=' . $sender->getUserID() . '">' . $sender->getUserName() . '</a>';
				$arrParam[] = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . 'index.php?view&type=item&id=' . $post->getItemID() . '">' . $post->getItem() . '</a>';
				$arrParam[] = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . '">' . $ubi['SERVER']['SITE_NAME'] . '</a>';
				SendMail::sendEMail($template, $subject, $arrParam, $ownerPost->getEmail());
			}
			
			// add score
			global $ubi_score;
			$this->_userBus->addScore($userID, $ubi_score['PLUS_MINUS']);
		}
		
		return $ck;
	}
	
	public function favourite($itemID, $userID) {
		global $ubi;

		$fav = new FavouriteModel();
		$fav->setCreatedDate($_SERVER['REQUEST_TIME']);
		$fav->setIndicteeID($itemID);
		$fav->setInteractType($ubi['ITEM']['VALUE']);
		$fav->setUserID($userID);
		
		$ck = $this->_favouriteDao->addFavourite($fav);

		if ($ck) {
			$update = new UpdateModel();
			$update->setIndicteeID($itemID);
			$update->setModifiedDate($fav->getCreatedDate());
			$update->setUpdateType($ubi['UPDATE']['FAVOURITE_ITEM']);
			$update->setUserID($userID);
			$update->setLink('index.php?view&type=item&id=' . $itemID);
			$update->setName($this->_itemDao->getItem($itemID)->getItem());
			
			$this->_updateDao->addUpdate($update);
			
			// send mail for the owner
			$post = $this->_itemDao->getItem($itemID);
			$ownerPost = $this->_userBus->getUser($post->getUserID());
			if (!in_array($ubi['EMAIL']['FAVOURITE_MY_POST'], $ownerPost->getEmailPrivacies())) {
				$sender = $this->_userBus->getUser($userID);
				SendMail::getEmailTemplate($ubi['EMAIL']['FAVOURITE_MY_POST'], $template, $subject);
				
				$arrParam = array();
				$arrParam[] = $ownerPost->getFirstName() . ' ' . $ownerPost->getLastName();	
				$arrParam[] = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . 'index.php?view&type=user&userid=' . $sender->getUserID() . '">' . $sender->getUserName() . '</a>';
				$arrParam[] = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . 'index.php?view&type=item&id=' . $post->getItemID() . '">' . $post->getItem() . '</a>';
				$arrParam[] = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . '">' . $ubi['SERVER']['SITE_NAME'] . '</a>';
				SendMail::sendEMail($template, $subject, $arrParam, $ownerPost->getEmail());
			}
			
			// add score for user favourite an item
			global $ubi_score;
			$this->_userBus->addScore($userID, $ubi_score['FAVOURITE_POST']);
		}
		
		return $ck;
	}
	
	public function removeFavourite($itemID, $userID) {
		global $ubi;

		$fav = new FavouriteModel();		
		$fav->setIndicteeID($itemID);
		$fav->setInteractType($ubi['ITEM']['VALUE']);
		$fav->setUserID($userID);
		
		$ck = $this->_favouriteDao->removeFavourite($fav);

		if ($ck) {
			$update = new UpdateModel();
			$update->setIndicteeID($providerID);
			$update->setModifiedDate($_SERVER['REQUEST_TIME']);
			$update->setUpdateType($ubi['UPDATE']['REMOVE_FAVOURITE_ITEM']);
			$update->setUserID($userID);
			$update->setLink('index.php?view&type=item&id=' . $itemID);
			$update->setName($this->_itemDao->getItem($itemID)->getItem());
			
			$this->_updateDao->addUpdate($update);
			
			// send mail for the owner
			$post = $this->_itemDao->getItem($itemID);
			$ownerPost = $this->_userBus->getUser($post->getUserID());
			if (!in_array($ubi['EMAIL']['UNFAVOURITE_MY_POST'], $ownerPost->getEmailPrivacies())) {
				$sender = $this->_userBus->getUser($userID);
				SendMail::getEmailTemplate($ubi['EMAIL']['UNFAVOURITE_MY_POST'], $template, $subject);
				
				$arrParam = array();
				$arrParam[] = $ownerPost->getFirstName() . ' ' . $ownerPost->getLastName();	
				$arrParam[] = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . 'index.php?view&type=user&userid=' . $sender->getUserID() . '">' . $sender->getUserName() . '</a>';
				$arrParam[] = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . 'index.php?view&type=item&id=' . $post->getItemID() . '">' . $post->getItem() . '</a>';
				$arrParam[] = '<a href="' . $ubi['SERVER']['SITE_ROOT'] . '">' . $ubi['SERVER']['SITE_NAME'] . '</a>';
				SendMail::sendEMail($template, $subject, $arrParam, $ownerPost->getEmail());
			}
		}
		
		return $ck;
	}
	
	public function getListImgs($listItem) {
		global $ubi;
		
		$imgs = array();
		$resBus = new ResourceBus();		
		
		for ($i = 0; $i < count($listItem); $i++) {
			$item = $listItem[$i];
			
			$resID = GeneralFunctions::getFirstElement($item->getResourceList());
			if ($resID !== false) {
				$img = $resBus->getImgOfResource($resID);
				$imgs[$item->getItemID()] = $img; 
			} else {
				$imgs[$item->getItemID()] = $ubi['ITEM']['DEFAULT_RESOURCE'];
			}		
		}
		
		return $imgs;
	}
	
	public function updateItem(ItemModel $itemModel) {
		$itemModel->setAnswerList(GeneralFunctions::stripString($itemModel->getAnswerList()));
		$itemModel->setBeginPrice(GeneralFunctions::stripString($itemModel->getBeginPrice()));
		$itemModel->setCategoryList(GeneralFunctions::stripString($itemModel->getCategoryList()));		
		$itemModel->setEndPrice(GeneralFunctions::stripString($itemModel->getEndPrice()));
		$itemModel->setItem(GeneralFunctions::stripString($itemModel->getItem()));
		
		if ($itemModel->getBeginPrice() || $itemModel->getEndPrice()) {
			$itemModel->setDateOfPrice($_SERVER['REQUEST_TIME']);
		}
		
		$itemModel->setUnit(GeneralFunctions::stripString($itemModel->getUnit()));
		
		return $this->_itemDao->updateItem($itemModel);
	}
	
	public function incFrequency($itemID) {
		$this->_itemDao->incFrequency($itemID);
	}
	
	public function getNumberOfComment($itemID) {
		return $this->_itemDao->getNumberOfComments($itemID);
	}
	
	public function getNumberOfFavourites($itemID) {
		return $this->_itemDao->getNumberOfFavourites($itemID);
	}
	
	public function getItemMaxFrequency() {
		return $this->_itemDao->getItemMaxFrequency();
	}
	
	public function getNewestItems() {
		return $this->_itemDao->getNewestItems();
	}
	
	public function getRandomItems() {
		global $ubi;
		
		return $this->_itemDao->getRandomItems($ubi['SITE']['NUMBER_N_RANDOM']);
	}	
	
	public function searchItems($searchText) {
		return $this->_itemDao->searchItems($searchText);
	}
	
	public function getItemsOfProvider($providerID) {
		return $this->_itemDao->getItemsOfProvider($providerID);
	}
	
	public function searchAdvancedItems($itemName, $beginPrice, $endPrice, $unit) {
		return $this->_itemDao->searchAdvancedItems($itemName, $beginPrice, $endPrice, $unit);
	}
	
	public function searchItemsByRegion($region) {
		return $this->_itemDao->searchItemsByRegion($region);	
	}
}
?>